package com.loto.graphics;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.ParseException;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.text.MaskFormatter;

import com.loto.model.CombinaisonModele;


/**
 * 
 * Creer le Panel du Jeu Contenant 3 différents Panel -L'entete -L'affichage de
 * la zone de saisie -L'affichage des boutons
 * 
 */
public class Game extends JFrame implements ActionListener {
	private JPanel entete;
	private JPanel combi;
	private JPanel bouton;
	private JPanel clavier;
	private JFormattedTextField un;
	private JFormattedTextField deux;
	private JFormattedTextField trois;
	private JFormattedTextField quatre;
	private JFormattedTextField cinq;
	private JFormattedTextField joker;
	private static Game instance;
	private CombinaisonModele combinaison;

	private Game() throws ParseException, IOException {
		combinaison = new CombinaisonModele();
		JPanel panneau = new JPanel(new GridLayout(4, 1));
		this.entete = this.entete();
		this.combi = this.champdesaisie();
		this.bouton = bouton();
		this.clavier = clavier();
		panneau.add(this.entete);
		panneau.add(combi);
		panneau.add(clavier);
		panneau.add(bouton);
		this.setTitle("Game");
		this.getContentPane().add(panneau);
		this.pack();
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

	}

	private JPanel entete() {
		GridLayout grille = new GridLayout(1, 2);
		JPanel entete = new JPanel(grille);
		JPanel title = new JPanel(new GridLayout(2, 1));
		JLabel titre = new JLabel("Générateur de grille");
		JLabel label2 = new JLabel("Loto");
		title.add(titre);
		title.add(label2);
		ImageIcon img = new ImageIcon("./logo.jpg");
		Image image = img.getImage();
		Image newimg = image.getScaledInstance(200, 100,
				java.awt.Image.SCALE_SMOOTH);
		ImageIcon newicon = new ImageIcon(newimg);
		JLabel logo = new JLabel(newicon);
		entete.add(title);
		entete.add(logo);
		return entete;
	}

	private JPanel champdesaisie() throws ParseException {
		JPanel champsaisie = new JPanel(new GridLayout(1, 2));
		JPanel combi = combinaison();
		JPanel joker = joker();
		Border cadre = BorderFactory
				.createTitledBorder("Entrez votre combinaison");
		combi.setBorder(cadre);
		champsaisie.add(combi);
		Border cadre2 = BorderFactory.createTitledBorder("Entrez votre numéro Chance");
		joker.setBorder(cadre2);
		champsaisie.setPreferredSize(new Dimension(200, 10));
		champsaisie.add(joker);
		return champsaisie;
	}

	/**
	 * Retourne le champ de saisie de la combinaisons
	 * 
	 * @return
	 * @throws ParseException
	 */
	private JPanel combinaison() throws ParseException {
		JPanel combinaison = new JPanel(new FlowLayout(FlowLayout.CENTER));
		try {
			MaskFormatter format = new MaskFormatter("##");
			format.setValidCharacters("0123456789");
			this.un = new JFormattedTextField(format);
			this.deux = new JFormattedTextField(format);
			this.trois = new JFormattedTextField(format);
			this.quatre = new JFormattedTextField(format);
			this.cinq = new JFormattedTextField(format);
		} catch (ParseException e) {

		}
		this.un.setPreferredSize(new Dimension(20, 20));
		this.deux.setPreferredSize(new Dimension(20, 20));
		this.trois.setPreferredSize(new Dimension(20, 20));
		this.quatre.setPreferredSize(new Dimension(20, 20));
		this.cinq.setPreferredSize(new Dimension(20, 20));
		combinaison.add(un);
		combinaison.add(deux);
		combinaison.add(trois);
		combinaison.add(quatre);
		combinaison.add(cinq);
		return combinaison;
	}

	/**
	 * Creation du champ texte pour le Joker
	 * 
	 * @return Joker
	 */
	private JPanel joker() {
		JPanel jokerpanel = new JPanel();
		MaskFormatter format;
		try {
			format = new MaskFormatter("##");
			format.setValidCharacters("0123456789");
			this.joker = new JFormattedTextField(format);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.joker.setPreferredSize(new Dimension(20, 20));
		jokerpanel.add(this.joker);
		return jokerpanel;
	}

	/**
	 * Retourne le panel de bouton
	 * 
	 * @return
	 */
	private JPanel bouton() {
		JPanel bouton = new JPanel(new FlowLayout());
		JButton validation = new JButton("validation",
				new ImageIcon("./ok.png"));
		validation.addActionListener(this);

		bouton.add(validation);
		JButton retour = new JButton("Retour", new ImageIcon("./annuler.png"));
		retour.addActionListener(this);
		bouton.add(retour);
		return bouton;
	}

	private JPanel clavier() {
		JPanel clavier = new JPanel(new GridLayout(7, 7));
		for (int i = 1; i <= 49; i++) {
			JButton touche = new JButton("" + i + "");
			touche.addActionListener(this);
			clavier.add(touche);
		}

		return clavier;
	}

	/**
	 * Retourne l'entete crée
	 * 
	 * @return JPanel l'entete
	 */
	public JPanel getentete() {
		return this.entete;
	}

	/**
	 * Retourne le champ d'entrée crée
	 * 
	 * @return JPanel l'entete
	 */
	public JPanel getcombi() {
		return this.combi;
	}

	public static Game getInstance() throws ParseException, IOException {
		if (instance == null) {
			instance = new Game();
		}
		return instance;
	}

	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() instanceof JButton) {

			JButton btn = (JButton) arg0.getSource();
			String buttontext = btn.getText();
			System.out.println(this.combinaison.getcombinaisonexist("6-16-22-31-39+7 "));
			if (buttontext.equalsIgnoreCase("Validation")) {
				if (this.combinaison.getcombinaisonexist(this.un.getText().trim()
						+ "-" + this.deux.getText().trim() + "-"
						+ this.trois.getText().trim() + "-" + this.quatre.getText().trim()
						+ "-" + this.cinq.getText().trim() + "+"
						+ this.joker.getText().trim())) {

					JOptionPane.showMessageDialog(this, "Win",
							buttontext, JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(this, "Lose",
							buttontext, JOptionPane.INFORMATION_MESSAGE);

				}
			} else if (buttontext.equalsIgnoreCase("Retour")) {
				this.setVisible(false);
			} else {
				if (this.un.getText().trim().isEmpty()) {
					this.un.setText(buttontext);
				} else if (this.deux.getText().trim().isEmpty()) {
					this.deux.setText(buttontext);
				} else if (this.trois.getText().trim().isEmpty()) {
					this.trois.setText(buttontext);
				} else if (this.quatre.getText().trim().isEmpty()) {
					this.quatre.setText(buttontext);
				} else if (this.cinq.getText().trim().isEmpty()) {
					this.cinq.setText(buttontext);
				} else if (this.joker.getText().trim().isEmpty()) {
					this.joker.setText(buttontext);
				}
			}
		}

	}
}